﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCodeCreater.Module
{
    public class CreateDomain
    {
        public static void Create(DataTable table, TableInfo tableinfo, string idomainContent, string domainContent,string irepositoryContent,string listSpecificationConten, string root)
        {
            idomainContent = idomainContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            domainContent = domainContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            irepositoryContent = irepositoryContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            listSpecificationConten= listSpecificationConten.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);

            Tool.SaveFile(root + $@"\Domain\Interface\I{tableinfo.name}DomainService.cs", idomainContent);
            Tool.SaveFile(root + $@"\Domain\Interface\I{tableinfo.name}Repoistory.cs", irepositoryContent);
            Tool.SaveFile(root + $@"\Domain\Services\{tableinfo.name}DomainService.cs", domainContent);


            var replace = new string[] {"ID", "CreateTime", "IsDeleted"};
            

            var keyforeach = new StringBuilder();
            var kayeqvalforeach = new StringBuilder();
            var satisfiedforeach = new StringBuilder();
            var index = 0;
            foreach (DataRow dataRow in table.Rows)
            {
                if (!replace.Contains(dataRow[0]))
                {
                    var type = Tool.GetCsharpTypeInSql(dataRow[1].ToString(), dataRow[2].ToString(), out Type strType);
                    keyforeach.AppendLine(string.Concat(new object[]
                    {
                        "        public ",
                        type,
                        (type=="string")?" ":"? ",
                        dataRow[0],
                        " { get; set; }"
                    }));
                    kayeqvalforeach.AppendLine("            " + dataRow[0] + " = " + "input." + dataRow[0] + ";");
                    switch (type)
                    {
                        case "string":
                            satisfiedforeach.AppendLine($"            if (!string.IsNullOrEmpty({dataRow[0]}))");
                            satisfiedforeach.AppendLine("            {");
                            satisfiedforeach.AppendLine($"                where = where.And(x => x.{dataRow[0]}.Contains({dataRow[0]}));");
                            satisfiedforeach.AppendLine("            }");
                            break;
                        default:
                            satisfiedforeach.AppendLine($"            if ({dataRow[0]} != null)");
                            satisfiedforeach.AppendLine("            {");
                            satisfiedforeach.AppendLine($"                where = where.And(x => x.{dataRow[0]} == {dataRow[0]});");
                            satisfiedforeach.AppendLine("            }");
                            break;
                    }
                }
            }
            listSpecificationConten = listSpecificationConten.Replace("_keyforeach_", keyforeach.ToString()).Replace("_kayeqvalforeach_", kayeqvalforeach.ToString())
                .Replace("_satisfiedforeach_", satisfiedforeach.ToString());
            Tool.SaveFile(root + $@"\Domain\Specification\{tableinfo.name}ListSpecification.cs", listSpecificationConten);
        }
    }
}
